package com.ghostbusters.fingertrack.match;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ghostbusters.fingertrack.calibration.Calibration;
import com.ghostbusters.fingertrack.calibration.Signature;
import com.ghostbusters.fingertrack.gui.MainFrame;
import com.ghostbusters.fingertrack.recognition.Recognizer;
import com.ghostbusters.fingertrack.wii.IRPoint;
import com.ghostbusters.fingertrack.wii.WiiMain;
import com.ghostbusters.fingertrack.wii.WiiSubscriber;
import com.ghostbusters.fingertracker.util.MapUtil;

/**
 * This is where all the matching content goes
 * 
 * @author geraldfong
 * 
 */
public class Matcher<E> implements WiiSubscriber {

	private Calibration<E> calibration;
	private MainFrame mainFrame;
	private Signature signature;
	private int counter = 0;
	private Recognizer<E> recognizer;

	public Matcher() {

	}

	public Matcher(MainFrame mainFrame, Calibration<E> calibration) {
		setMainFrame(mainFrame);
		setCalibration(calibration);
		setRecognizer(new Recognizer<E>());
		setSignature(new Signature());

		WiiMain.subscribe(this);
	}

	public void setCalibration(Calibration<E> calibration) {
		this.calibration = calibration;
	}

	public Calibration<E> getCalibration() {
		return calibration;
	}

	@Override
	public void newPoints(List<IRPoint> irPoints) {
		signature.addPoint(new IRPoint(irPoints.get(0).getX()
				* Calibration.CALIBRATION_SIZE, irPoints.get(0).getY()
				* Calibration.CALIBRATION_SIZE));
		double min = Double.MAX_VALUE;
		Map<Character, Double> value = null, lastVal = null;
		// for (int i = 0; i < signature.getList().size() - 20; i = i + 5) {
		// List list = (new MapUtil()).splitFrom(getSignature().getList(), 0);
		// Signature sig = new Signature();
		// sig.setList(list);

		value = (new MapUtil<Character>())
				.findMin(new MapUtil().findBelowThreshold(getRecognizer()
						.match(getCalibration(), getSignature()), 0.2));
		if (lastVal != null)
			for (Character c : lastVal.keySet()) {
				if (value.get(c) != null) {
					counter++;
					if (counter == 5) {
						signature = new Signature();
						getMainFrame().printMatches(c.toString());
					}
				} else {
					counter = 0;
				}
			}
		if (!value.isEmpty())
			for (Character object : value.keySet()) {
				try {
					if (value.get(object) < min) {
						min = value.get(object);
						value = new HashMap<Character, Double>();
						value.put(object, min);
					}
				} catch (Throwable e) {

				}
			}
		lastVal = value;

		// TODO: stuff here

	}

	public void setRecognizer(Recognizer<E> recognizer) {
		this.recognizer = recognizer;
	}

	public Recognizer<E> getRecognizer() {
		return recognizer;
	}

	public Signature getSignature() {
		return signature;
	}

	public void setSignature(Signature signature) {
		this.signature = signature;
	}

	public void end() throws Exception {
		WiiMain.unsubscribe(this);

		setSignature(new Signature());
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	public MainFrame getMainFrame() {
		return mainFrame;
	}
}
